Generated code - The predicate system, SelfServicing

Preface

LLBLGen Pro's filtering capabilities are build using predicate objects, instantiated from predicate classes. This section describes these classes in depth, how to use them, what their purpose is and shows an example for every predicate.

To quickly understand which predicate class you need, a handy table is provided below, which should help you converting a WHERE construct in SQL to LLBLGen Pro predicates. These predicate classes are usable in queries on the database and often also in-memory. Below the table a list of predicate classes is given which are solely usable for in-memory filtering.

Predicate classes to use for Database or in-memory filtering:
SQL Predicate class to use
Field BETWEEN 3 AND 5
Field BETWEEN field2 AND 4
FieldBetweenPredicate
Field = Field2
Field < (Field2 * 4)
FieldCompareExpressionPredicate
Field Is NULL FieldCompareNullPredicate
Field IN (1, 2, 3, 5) FieldCompareRangePredicate
Field IN (
SELECT Field FROM Foo WHERE ...)
FieldCompareSetPredicate
Field = 3
Field != "Foo"
FieldCompareValuePredicate
Field LIKE "Foo%" FieldLikePredicate

Predicate classes to use for in-memory filtering only:

The predicate classes

LLBLGen Pro offers you a wide range of different predicate classes to define your filters with. Each predicate class which name starts with Field works on a field, so they all have the form field some expression/operator values. Sometimes this is not enough, you for example want to filter using the predicate (Field * 3) > OtherField. This can be accomplished by adding an expression to the field filtered on. See Field expressions and aggregates for more information about expressions.

All predicate classes implement the IPredicate interface and derive from the Predicate class located, as the predicate classes, in the ORMSupportClasses assembly. If you want to add a specific predicate to the pack already offered, you can: implement a class also deriving from Predicate and you're done.

To see the full class signatures and their methods, please consult the LLBLGen Pro reference manual's SD.LLBLGen.Pro.ORMSupportClasses namespace. The various examples below utilize the previously discussed entitynameFields classes to create new entity fields to work with the predicate classes. You can also specify existing fields from an entity instance, so these three lines are equivalent:

// C#
IEntityField field = EntityFieldFactory.Create(OrderFieldIndex.OrderDate);
EntityField field = OrderFields.OrderDate;
IEntityField field = myOrder.Fields[(int)OrderFieldIndex.OrderDate];
' VB.NET
Dim field As IEntityField = EntityFieldFactory.Create(OrderFieldIndex.OrderDate)
Dim field As EntityField = OrderFields.OrderDate;
Dim field As IEntityField = myOrder.Fields(Cint(OrderFieldIndex.OrderDate))

Please note that the entitynameFields.Fieldname constructors return an EntityField type, not an IEntityField type, to be sure operators work on them (see next section).

In-memory filtering and entity properties
To filter an EntityView you use in-memory filtering using normal predicate classes, be it the Field* classes or the predicate classes for in-memory filtering. To filter the entities accessable through the EntityView, you can use normal entity fields obtained with the constructs mentioned above. However for filtering on properties which aren't fields, you can't use EntityField instances. To use normal predicate classes to filter on these properties, only in-memory, you can use the class EntityProperty. This class behaves like an EntityField in predicate objects and Expression objects when they're used for in-memory filtering. An example of EntityProperty in action can be found in Generated code - using the EntityView class.
Native language filter construction


note Note:
The feature discussed in this paragraph is not available for VB.NET 2002/2003 users, because VB.NET 2002/2003 doesn't support operator overloading, and users of VB.NET 2002/2003 have to fall back on the more verbose way to constructing filters. VB.NET 2005 supports operator overloading in full and thus also this feature. C# 2002/2003 supports operator overloading.

Getting started with filtering is the ability to formulate filters using compact, native language constructs. In the following sub-sections, which each discuss a predicate class, examples will be given how filters can be formulated using the native language constructs supported by LLBLGen Pro. It doesn't stop there, you can also construct sort expressions and predicate expressions using native language constructs. The following paragraphs will show you how this is performed. Please note that the VB.NET examples only work in VB.NET 2005.

Constructing Predicate Expressions
In the paragraph Predicates and Predicate expressions, you were introduced to the concepts of predicates and predicate expressions. The following examples will show equivalents of the earlier examples in that paragraph to illustrate how to use native language contstructs to create predicate expressions. These predicate expressions are created by the overloaded operators & and | in C# and And and Or in VB.NET 2005. To produce the same full filter as illustrated in Predicates and Predicate expressions, use the following code. It uses a single step, which skips the separate creation of filter A.

// C#
IPredicateExpression B = ((Table1Fields.Foo == "One") & (Table1Fields.Bar == "Two")) // A
	| (Table2Fields.Bar2 == "Three");
pre>' VB.NET 2005 Dim B IPredicateExpression = ((Table1Fields.Foo = "One") And (Table1Fields.Bar = "Two")) _ ' A Or (Table2Fields.Bar2 = "Three")
<
It's also possible to negate a predicate with the native language operator ! (C#) or Not (VB.NET 2005). Same example as previously, but now the last predicate is negated:

// C#
IPredicateExpression B = ((Table1Fields.Foo == "One") & (Table1Fields.Bar == "Two")) // A
	| !(Table2Fields.Bar2 == "Three");
' VB.NET 2005
Dim B IPredicateExpression = ((Table1Fields.Foo = "One") And (Table1Fields.Bar = "Two")) _  ' A
	Or Not (Table2Fields.Bar2 = "Three")

To chain several predicates together into a single predicate expression, you can also consider the AddWithAnd and AddWithOr methods of the PredicateExpression object. Every (predicate Operator predicate) statement results in a PredicateExpression object. If you want to do this: (the example shows values in the WHERE clause, LLBLGen Pro always generates parameters for values, it never includes any value into the query directly)

...
WHERE	TableFields.Foo = "One" OR 
		TableFields.Foo = "Two" OR 
		TableFields.Foo = "Three" OR
		TableFIelds.Foo = "Four"

you should use this code:

// C#
IPredicateExpression filter = 
	((TableFields.Foo=="One") | (TableFields.Foo=="Two"))
	.AddWithOr(TableFields.Foo=="Three")
	.AddWithOr(TableFields.Foo=="Four);
	
// which is equal to:
IPredicateExpression filter = new PredicateExpression();
filter.Add(TableFields.Foo=="One")
	.AddWithOr(TableFields.Foo=="Two")
	.AddWithOr(TableFields.Foo=="Three")
	.AddWithOr(TableFields.Foo=="Four);
	
// which is equal to:
IPredicateExpression filter = new PredicateExpression();
filter.Add(new FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "One"))
	.AddWithOr(new FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Two"))
	.AddWithOr(new FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Three"))
	.AddWithOr(new FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Four"));
' VB.NET 2005
Dim filter As IPredicateExpression = _
	((TableFields.Foo="One") Or (TableFields.Foo="Two")).AddWithOr(TableFields.Foo="Three").AddWithOr(TableFields.Foo="Four)
	
' which is equal to: (VB.NET 2005)
Dim filter As New PredicateExpression()
filter.Add(TableFields.Foo="One").AddWithOr(TableFields.Foo="Two").AddWithOr(TableFields.Foo="Three").AddWithOr(TableFields.Foo="Four)
	
' which is equal to: (VB.NET 2002/2003/2005)
Dim filter As New PredicateExpression()
' NOTE: the following line is specified on multiple lines here for readability, VB.NET wants you to mention the statements on 1 line
filter.Add(New FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "One")) _
	.AddWithOr(New FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Two")) _
	.AddWithOr(New FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Three")) _
	.AddWithOr(New FieldCompareValuePredicate(TableFields.Foo, ComparisonOperator.Equal, "Four"))

Fields and operators
Various operators are defined to work with the EntityField objects to form filters, or even expressions (See for expressions: Field expressions and aggregates). The list below will guide you what kind of object is created when you use the specified operator on an EntityField object. An example of the usage is given as well. It's recommended you consult the LLBLGen Pro reference manual, located in your LLBLGen Pro installation folder, and check which operators are defined on which classes, for example the operators on SD.LLBLGen.Pro.ORMSupportClasses.EntityField. For example the '==' / '=' operator (Equality) can produce different predicate object types, depending on the right hand side value: if it's another EntityField object or an Expression, a FieldCompareExpressionPredicate is produced, if it's null / Nothing, a FieldCompareNullPredicate is produced. See the reference manual and the predicate classes below for more details.

C# VB.NET 2005 Descr. Example Object produced
+ + Addition (OrderDetailsFields.Quantity + 10) Expression
| Or SortClause construction (CustomerFields.CompanyName | SortOperator.Ascending) SortClause
/ / Division (OrderDetailsFields.Quantity / 10) Expression
== = Equality (CustomerFields.CompanyName == "Foo Inc.") FieldCompareValuePredicate, FieldCompareExpressionPredicate, FieldCompareRangePredicate, FieldCompareNullPredicate
> > Greater Than (OrderDetailsFields.Quantity > 10) FieldCompareValuePredicate, FieldCompareExpressionPredicate
>= >= Greater Than Or Equal (OrderDetailsFields.Quantity >= 10) FieldCompareValuePredicate, FieldCompareExpressionPredicate
!= <> Equality (CustomerFields.CompanyName != "Foo Inc.") FieldCompareValuePredicate, FieldCompareExpressionPredicate, FieldCompareRangePredicate, FieldCompareNullPredicate
< < Lesser Than (OrderDetailsFields.Quantity < 10) FieldCompareValuePredicate, FieldCompareExpressionPredicate
<= <= Lesser Than Or Equal (OrderDetailsFields.Quantity <= 10) FieldCompareValuePredicate, FieldCompareExpressionPredicate
% Mod Like creation (CustomerFields.CompanyName % "Foo%") FieldLikePredicate
* * Multiplication (OrderDetailsFields.Quantity * 10) Expression
- - Substraction (OrderDetailsFields.Quantity - 10) Expression

Not all predicate classes can be created with the operator overloads, for example FieldBetweenPredicate and FieldCompareSetPredicate aren't constructable using operator overloads like discussed in the previous table. Nevertheless, using the field construction classes and the Set*() methods, it will be easy to construct these predicates as well.

Mixing of construction ways
Because the combination of an EntityField and an operator produces a normal object like a FieldCompareValuePredicate or an Expression object, you can mix the construction ways in your code. So for example, the easy way to construct predicate expressions shown earlier, works also on objects created using the predicate classes directly, as shown in the following example which shows a mix of the two techniques:

// C#
PredicateExpression filter = (OrderFields.CustomerID == "CHOPS") &
	(new FieldBetweenPredicate(OrderFields.ShippedDate, dateStart, dateEnd));
' VB.NET
Dim filter As PredicateExpression = (OrderFields.CustomerID = "CHOPS") And _
	(New FieldBetweenPredicate(OrderFields.ShippedDate, dateStart, dateEnd))

Predicate classes for database queries or in-memory filtering

FieldBetweenPredicate

note Note:
The SQL examples given can contain absolute values. The SQL generated by the predicates will never contain absolute values as absolute values will be converted to parameters.

Description compares the entity field specified using a BETWEEN operator and results in true if the field's value is greater than or equal to the value of valueBegin and less than or equal to the value of valueEnd. valueBegin and valueEnd can be any value or an EntityField, as shown in the examples.
SQL equivalent Field BETWEEN valueStart AND valueEnd
Field BETWEEN OtherField AND valueEnd
Field BETWEEN valueStart AND OtherField
Operators none.
Example

  • C#
  • VB.NET
// C#
filter.Add(new FieldBetweenPredicate( OrderFields.OrderDate, dateStart, dateEnd));
// or: ShippingDate BETWEEN RequiredDate and dateEnd
filter.Add(new FieldBetweenPredicate( OrderFields.ShippingDate, OrderFields.RequiredDate, dateEnd));
' VB.NET
filter.Add(new FieldBetweenPredicate( OrderFields.OrderDate, dateStart, dateEnd))
' or: ShippingDate BETWEEN RequiredDate and dateEnd
filter.Add(new FieldBetweenPredicate( OrderFields.ShippingDate, OrderFields.RequiredDate, dateEnd))

Can be used for in-memory filtering Yes
FieldCompareExpressionPredicate
Description compares the entity field specified with the expression specified, using the ComparisonOperator specified. See for a detailed description about expressions the Field expressions and aggregates section
SQL equivalent examples Field > (OtherField * 2)
Field <= OtherField
Operators All ComparisonOperator operators: Equal, GreaterEqual, GreaterThan, LessEqual, LesserThan, NotEqual
Example This example creates a predicate which compares Order.OrderDate with Order.ShippingDate. The example might look a little verbose, but Expression objects are re-usable, which allows you to define the Expression objects once and re-use them each time you need them with a predicate class for example. The example illustrates a very basic expression, but the expression you can specify can be very complex. See for more information about expressions Field expressions and aggregates

  • C#
  • VB.NET
// C#
filter.Add(new FieldCompareExpressionPredicate(
	OrderFields.OrderDate, ComparisonOperator.Equal,
	new Expression(OrderFields.ShippedDate)));
	
// which is equal to:
filter.Add((OrderFields.OrderDate == OrderFields.ShippedDate));
' VB.NET
filter.Add(New FieldCompareExpressionPredicate( _
	OrderFields.OrderDate, ComparisonOperator.Equal, _
	New Expression(OrderFields.ShippedDate)))
	
' which is equal to: (VB.NET 2005)
filter.Add((OrderFields.OrderDate = OrderFields.ShippedDate))

Example which filters on orders which have been shipped 4 days after the orderdate:

  • C#
  • VB.NET
// C#
filter.Add(new FieldCompareExpressionPredicate(
	OrderFields.ShippedDate, ComparisonOperator.Equal,
	new Expression(OrderFields.OrderDate, ExOp.Add, 4)));
	
// which is equal to:
filter.Add((OrderFields.ShippedDate == (OrderFields.OrderDate + 4)));
' VB.NET
filter.Add(New FieldCompareExpressionPredicate( _
	OrderFields.ShippedDate, ComparisonOperator.Equal, _
	New Expression(OrderFields.OrderDate, ExOp.Add, 4)))
	
' which is equal to: (VB.NET 2005)
filter.Add((OrderFields.ShippedDate == (OrderFields.OrderDate + 4)))

Can be used for in-memory filtering Yes
FieldCompareNullPredicate
Description compares the entity field specified with NULL.
SQL equivalent Field IS NULL
Operators none.
Example

  • C#
  • VB.NET
// C#
filter.Add(new FieldCompareNullPredicate(OrderFields.OrderDate));
	
// which is equal to:
filter.Add((OrderFields.OrderDate==System.DBNull.Value));
' VB.NET
filter.Add(New FieldCompareNullPredicate(OrderFields.OrderDate))
	
' which is equal to: (VB.NET 2005)
filter.Add((OrderFields.OrderDate = System.DBNull.Value))

Can be used for in-memory filtering Yes
FieldCompareRangePredicate
Description compares the entity field specified with the range of specified values using the IN operator. The range is not a subquery, use FieldCompareSetPredicate for that. The range can be supplied in an ArrayList, in an array or hardcoded in the predicate constructor.
SQL equivalent examples Field IN (1, 2, 5, 10)
Field IN ("Foo", "Bar", "Blah")
Operators none.
Example This example creates a predicate which compares Order.EmployeeId with the range 1, 2, 5, stored in an array. The values can also be specified directly in the constructor.

  • C#
  • VB.NET
// C#
int[] values = new int[3] {1, 2, 5};
filter.Add(new FieldCompareRangePredicate(OrderFields.EmployeeId, values));

// which is equal to:
filter.Add(OrderFields.EmployeeId == values);
' VB.NET
Dim values As Integer() = New Integer(2) {1, 2, 5}
filter.Add(New FieldCompareRangePredicate(OrderFields.EmployeeId, values))

' which is equal to (VB.NET 2005)
filter.Add(OrderFields.EmployeeId = values)

Can be used for in-memory filtering Yes
FieldCompareSetPredicate
Description compares the entity field specified with the set of values defined by the subquery elements, using the SetOperator specified. The FieldCompareSetPredicate is the predicate you'd like to use when you want to compare a field's value with a range of values retrieved from another table / view (or the same table / view) using a subquery.

FieldCompareSetPredicates also allows you to define EXISTS () queries. It is then not necessary to specify an IEntityField object with the predicate's constructor (specify null / nothing) as it is ignored when building the SQL. Keep in mind that EXISTS() queries are semantically the same as IN queries and IN queries are often simpler to formulate.

The FieldCompareSetPredicate supports advanced comparison operators like ANY, ALL and combinations of these with comparison operators like Equal (=) or GreaterThan (>). If the set is just 1 value in size (because you've specified a limit on the number of rows to return), it's wise to use the Equal operator instead of the IN operator as most databases will be rather slow with IN and just 1 value compared to the Equal operator.
SQL equivalent examples Field IN (SELECT OtherField FROM OtherTable WHERE Foo=2)
EXISTS (SELECT * FROM OtherTable)
Operators All SetOperator operators: In, Exists, Equal, EqualAny, EqualAll, LessEqual, LessEqualAny, LessEqualAll, LesserThan, LesserThanAny, LesserThanAll, GreaterEqual, GreaterEqualAny, GreaterEqualAll, GreaterThan, GreaterThanAny, GreaterThanAll, NotEqual, NotEqualAny, NotEqualAll
Example This example illustrates the query: Customer.CustomerID IN (SELECT CustomerID FROM Orders WHERE Employee=2)

  • C#
  • VB.NET
// C#
filter.Add(new FieldCompareSetPredicate(
	CustomerFields.CustomerID, OrderFields.CustomerID,
	SetOperator.In, (OrderFields.EmployeeID == 2)));
' VB.NET
filter.Add(New FieldCompareSetPredicate( _
	CustomerFields.CustomerID, OrderFields.CustomerID, _
	SetOperator.In, _
	New FieldCompareValuePredicate(OrderFields.EmployeeID, _
		ComparisonOperator.Equal, 2)))

' which is equal to: (VB.NET)
filter.Add(New FieldCompareSetPredicate( _
	CustomerFields.CustomerID, OrderFields.CustomerID, _
	SetOperator.In, (OrderFieldIndex.EmployeeID = 2)))

Can be used for in-memory filtering No
FieldCompareValuePredicate
Description compares the entity field specified with the value specified, using the ComparisonOperator specified. If the value to compare is a string, you will get a case sensitive compare if the database is using a case sensitive collation (like Oracle). You can perform case insensitive compares however, by setting the CaseSensitiveCollation property to true prior to passing the predicate to a fetch method like GetMulti(). This will perform the UPPERCASE variant of the field with the pattern specified. Please note that if you've set CaseSensitiveCollaction to true, you've to specify your pattern in uppercase as well.
SQL equivalent examples Field > 3
Field = "Foo"
Operators All ComparisonOperator operators: Equal, GreaterEqual, GreaterThan, LessEqual, LesserThan, NotEqual
Example This example creates a predicate which compares Order.EmployeeID with the value 2

  • C#
  • VB.NET
// C#
filter.Add(new FieldCompareValuePredicate(OrderFields.EmployeeID, 
	ComparisonOperator.Equal, 2));
	
// which is equal to:
filter.Add(OrderFields.EmployeeID == 2);
' VB.NET
filter.Add(New FieldCompareValuePredicate(OrderFields.EmployeeID, _
	ComparisonOperator.Equal, 2))

' which is equal to:
filter.Add(OrderFields.EmployeeID = 2)

Can be used for in-memory filtering Yes.

note Note:
When using FieldCompareValuePredicate for in-memory filters, like with an EntityView, and you're using a value which is of a different type than the type of the field, the value to compare with is converted to the type of the field before the comparison. For example, if the field is of type Int64, and you specify as value to compare the value 1, you'll be comparing an Int64 with an Int32, which will result in the conversion of the Int32 value '1' to an Int64 value. If you don't want this conversion to happen, specify the value in the type of the field.

FieldFullTextSearchPredicate (SqlServer specific)
Description SqlServer specific. Compares the entity field specified with the pattern specified using the FullTextSearch operator specified. On SqlServer 2005, multi-field CONTAINS / FREETEXT compares are supported. See the example below.
SQL equivalent CONTAINS(Field, "Bla") FREETEXT(Field, "Bla")
Operators All FullTextSearchOperator operators: Contains, Freetext
Example The following example searches using SqlServer's Full Text search in the CustomerEntity's field CompanyName for the string "Solution"

  • C#
  • VB.NET
// C#
filter.Add(new FieldFullTextSearchPredicate(
	CustomerFields.CompanyName,	FullTextSearchOperator.Contains, "Solution"));
' VB.NET
filter.Add(new FieldFullTextSearchPredicate(
	CustomerFields.CompanyName,	FullTextSearchOperator.Contains, "Solution"))

The next example shows a filter on two fields, using the SqlServer 2005 specific feature to accept multiple fields for the same operator.

  • C#
  • VB.NET
// C#
filter.Add(new FieldFullTextSearchPredicate(
	new IEntityField[] { CustomerFields.CompanyName, CustomerFields.ContactName},
	FullTextSearchOperator.Contains, "Solution"));
' VB.NET
filter.Add(New FieldFullTextSearchPredicate( _
	New IEntityField() { CustomerFields.CompanyName, CustomerFields.ContactName }, _
	FullTextSearchOperator.Contains, "Solution"))

Can be used for in-memory filtering No, use the FieldLikePredicate with a regular expression for full-text search capabilities.
FieldLikePredicate
Description compares the entity field specified with the pattern specified, using the LIKE operator. The pattern should contain the wildcard, which is '%' (also for MS Access). FieldLikePredicate performs a LIKE compare using the case sensitivity setting of the database system the query is executed on: the SQL generated does not contain any collation information nor any case insensitive setting if the database is using case sensitive comparison operations by default (Oracle, some SqlServer installations). You can perform case insensitive compares however, if the database is case sensitive, by setting the CaseSensitiveCollation property to true prior to passing the predicate to a fetch method like GetMulti(). This will perform the UPPERCASE variant of the field with the pattern specified. Please note that if you've set CaseSensitiveCollaction to true, you've to specify your pattern in uppercase as well.
SQL equivalent examples Field LIKE '%bla'
Field LIKE 'bla%'
Operators none.
Example This example creates a predicate which compares Customer.CompanyName to the pattern "Solution%".

  • C#
  • VB.NET
// C#
filter.Add(new FieldLikePredicate(CustomerFields.CompanyName, "Solution%"));

// Which is equal to:
filter.Add(CustomerFields.CompanyName % "Solution%");
' VB.NET
filter.Add(New FieldLikePredicate(CustomerFields.CompanyName, "Solution%"))

' Which is equal to: (VB.NET 2005)
filter.Add(CustomerFields.CompanyName Mod "Solution%")

Note, that the operator syntaxis is a little odd in VB.NET, due to the fact that there isn't an ability to add new operators to VB.NET/C#.
Can be used for in-memory filtering Yes. When used in in-memory filters, the pattern can either be a normal LIKE statement pattern with '%' wildcards, or it can be a full regular expression. If the pattern is a regular expression, be sure to set the property PatternIsRegEx to true. See also the LLBLGen Pro reference manual on more detailed information about the properties of the FieldLikePredicate


Predicate classes for in-memory filtering only

Below you'll find the predicate classes which are only usable for in-memory filtering.
AggregateSetPredicate
Description Predicate class which performs an aggregate function on a set of entities and returns true or false depending if that aggregated value
matches a specified expression. The set of entities this predicate is applied on, for example a collection of OrderEntity instances, are the elements of a member property with the name specified which match the specified filter, for example 'Orders' in a set of CustomerEntity instances.
Operators All ComparisonOperator operators: Equal, GreaterEqual, GreaterThan, LessEqual, LesserThan, NotEqual
Example This example filters a set of customers to find all customers with at least 10 orders. customers is a collection of CustomerEntity instances.
  • C#
  • VB.NET
// C#
IPredicate filter = new AggregateSetPredicate(	
	CustomerEntity.MemberNames.Orders,	// member to apply the aggregate on
	AggregateSetFunction.Count,	// aggregate function to apply. 
	OrderFields.OrderId,		// value producer. Can be a field as well.
	ComparisonOperator.GreaterThan,	// comparison operator for the aggregateset 
	10,				// value to compare with
	null);			// additional filter to apply to the set.
		
IEntityView filteredCustomers = new EntityView<CustomerEntity>(customers, filter);
' VB.NET
Dim filter As IPredicate = New AggregateSetPredicate( _ 
	CustomerEntity.MemberNames.Orders, _ 	' member to apply the aggregate on
	AggregateSetFunction.Count,	_ 	' aggregate function to apply. 
	OrderFields.OrderId, _			' value producer. Can be a field as well.
	ComparisonOperator.GreaterThan,	_	' comparison operator for the aggregateset
	10, _			' value to compare with
	Nothing)		' additional filter to apply to the set. 
		
Dim filteredCustomers As IEntityView = New EntityView(Of CustomerEntity)(customers, filter)
DelegatePredicate and DelegatePredicate(Of T)
Description Predicate class to filter in-memory entity collections based on a specified callback function. Use this predicate to filter entities based on logic which is best expressed in a normal .NET language, like C# or VB.NET.

The generic variant accepts a Predicate(Of T) (.NET 2.0+) or Lambda expression (.NET 3.5) which is then used as a filter.
Operators None
Example This example filters a set of customers which have a CompanyName with length of 20 or higher. customers is a collection of CustomerEntity instances.
  • C#
  • VB.NET
// C#
// Create an entity view from customers based on the DelegatePredicate filter. 
// Uses an anonymous method, which is a .NET 2.0 feature.
IEntityView customersWithLongName = new EntityView<CustomerEntity>(customers,
	new DelegatePredicate(
	  delegate(IEntityCore toExamine) 
	  { 
		  return ((CustomerEntity)toExamine).CompanyName.Length > 20; 
	  }));
' VB.NET
' VB.NET doesn't support anonymous methods, so we need a separate method. 
Public Function CompareCustomerCompanyName(ByVal toExamine As CustomerEntity) As Boolean
	Return toExamine.CompanyName.Length > 20
End Function
' .. somewhere in your code where you want to filter on the CompanyName length Dim customersWithLongName As IEntityView = New EntityView(Of CustomerEntity)(customers, _ new DelegatePredicate(AddressOf CompareCustomerCompanyName))

For a .NET 3.5 example, please see the EntityView filtering example.
MemberPredicate
Description Predicate class which allows in-memory filters to perform a predicate (filter) on one or more related entities. The entity this predicate
is applied on has to have a member property with the name specified. Each element in that member (or the member itself, in case of a
single instance) will be interpreted with the specified filter. The result of that interpretation is used together with the MemberOperator specified what the result of this predicate will be: true or false, in which case the entity this predicate is applied on is accepted (true) or not (false).
Operators All MemberOperator operators: All, Any or Null (no data)
Example This example shows in memory filtering of customers which filters all customers which have orders with a total > 5000. The order total is calculated by SUMming the orderdetail total values. The order detail total is (UnitPrice * quantity) - ((UnitPrice * quantity)* discount). customers is a collection of CustomerEntity instances.

  • C#
  • VB.NET
// C#
IPredicate filter = new MemberPredicate(
    CustomerEntity.MemberNames.Orders,    // the member to apply the specified filter on.
    MemberOperator.Any,                   // operator for applying contained filter.
    new AggregateSetPredicate(            // aggregate predicate to apply on a member
        OrderEntity.MemberNames.OrderDetails,        // member to apply the aggregate on
        AggregateSetFunction.Sum,        // aggregate function to apply. 
        ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity)-
            ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity) 
                * OrderDetailsFields.Discount)),        // value producer expression. 
        ComparisonOperator.GreaterThan,    // comparison operator for the aggregateset 
        5000.0M,        // value to compare with
        null            // filter which specifies the set to apply the aggregate on.
        )                    
    );

IEntityView filteredCustomers = new EntityView<CustomerEntity>(customers, filter);
' VB.NET
Dim filter As IPredicate = New MemberPredicate( _
    CustomerEntity.MemberNames.Orders,    _ ' the member to apply the specified filter on.
    MemberOperator.Any, _                  ' operator for applying contained filter.
    New AggregateSetPredicate( _        ' aggregate predicate to apply on a member
        OrderEntity.MemberNames.OrderDetails, _        ' member to apply the aggregate on
        AggregateSetFunction.Sum, _        ' aggregate function to apply. 
        ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity)- _
            ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity)  _
                * OrderDetailsFields.Discount)), _        ' value producer expression. 
        ComparisonOperator.GreaterThan,    _    ' comparison operator for the aggregateset 
        5000.0D, _            ' value to compare with
        Nothing    _            ' filter which specifies the set to apply the aggregate on.
        ))

Dim filteredCustomers As new EntityView(Of CustomerEntity)(customers, filter)

LLBLGen Pro v2.6 documentation. ©2002-2008 Solutions Design